package com.datawin.taotaocheche.ui.actviity.user.presenter;

import android.text.TextUtils;

import javax.inject.Inject;

import com.datawin.taotaocheche.base.BaseBiz;
import com.datawin.taotaocheche.ui.actviity.user.biz.ResetPwdBiz;
import com.datawin.taotaocheche.ui.actviity.user.contract.ResetPwdContract;
import com.datawin.taotaocheche.util.StringUtils;
import com.datawin.taotaocheche.util.ValidatorUtil;

/**
 * Created by Administrator on 2017/10/21.
 */

public class ResetPwdPresenter implements ResetPwdContract.Presenter {

    ResetPwdContract.View view;
    ResetPwdBiz resetPwdBiz;


    @Inject
    public ResetPwdPresenter(ResetPwdContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        resetPwdBiz = (ResetPwdBiz) biz;
    }


    @Override
    public void ForgetPwd(String phone, String pwd, String newPwd, String code) {
        if (StringUtils.isEmpty(phone)) {
            view.showMsg("请输入手机号码");
            return;
        }
        if (StringUtils.isEmpty(pwd)) {
            view.showMsg("请输入新密码");
            return;
        }
        if (StringUtils.isEmpty(code)) {
            view.showMsg("请输入验证码");
            return;
        }
        if (!ValidatorUtil.isMobile(phone)) {
            view.showMsg("手机号码不正确");
            return;
        }
        if (!pwd.equals(newPwd)) {
            view.showMsg("两次输入的密码不一致");
            return;
        }
        view.loading(true);
        resetPwdBiz.forgetPwd(phone, pwd, code, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object o) {
                view.loading(false);
                view.showMsg("密码修改成功，请登录");
                view.finishAct();

            }

            @Override
            public void onFailure(Object o) {

            }
        });
    }

    @Override
    public void getCode(String phone) {
        if (TextUtils.isEmpty(phone)) {
            view.showMsg("手机号未填写");
            return;
        }
        if (!ValidatorUtil.isMobile(phone)) {
            view.showMsg("手机号码不正确");
            return;
        }
        view.loading(true);
        resetPwdBiz.mobileSms(phone, 2, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object info) {
                view.countDown();
                view.loading(false);
            }

            @Override
            public void onFailure(Object info) {
                view.loading(false);
            }
        });

    }
}
